<template>
  <div class="search-box">
    <!-- 搜索框 -->

    <van-search
      @input="input"
      v-model="value"
      show-action
      placeholder="开荒保洁"
      @search="onSearch"
      @cancel="onCancel"
    />
    <!-- 搜索建议列表 -->
    <div class="sugg-list" v-if="searchResults.length !== 0">
      <van-cell v-for="(item, index) in searchResults" :key="index">{{
        item
      }}</van-cell>
    </div>

    <!-- 搜索历史 -->
    <div class="history-box" v-else>
      <!-- 标题区域 -->
      <div class="history-title">
        <span>搜索历史</span>
        <van-icon name="delete-o" size="17" @click="cleanHistory" />
      </div>
      <!-- 列表区域 -->
      <div class="history-list">
        <van-tag
          plain
          type="danger"
          v-for="(item, i) in historys"
          :key="i"
          @click="gotoGoodsList(item)"
          >{{ item }}</van-tag
        >
      </div>
    </div>
  </div>
</template>

<script>
import { setItem, getItem } from "../../utils/storage";

export default {
  data() {
    return {
      timer: null,
      kw: "",
      value: "",
      searchResults: [],
      historyList: [],
    };
  },
  computed: {
    historys() {
      // 注意：由于数组是引用类型，所以不要直接基于原数组调用 reverse 方法，以免修改原数组中元素的顺序
      // 而是应该新建一个内存无关的数组，再进行 reverse 反转
      return [...this.historyList].reverse();
    },
  },
  created() {
    this.historyList = getItem("kw") || [];
  },
  methods: {
    input(e) {
      // 清除 timer 对应的延时器
      clearTimeout(this.timer);
      // 重新启动一个延时器，并把 timerId 赋值给 this.timer
      this.timer = setTimeout(() => {
        // 如果 500 毫秒内，没有触发新的输入事件，则为搜索关键词赋值
        this.kw = e.value;
        this.getSearchList();
      }, 500);
    },
    onSearch(val) {
      this.historyList.push(val);
    },
    onCancel() {
      this.$router.back();
    },
    getSearchList() {
      if (this.kw === "") {
        this.searchResults = [];
        return;
      }
      this.saveSearchHistory();
    },
    saveSearchHistory() {
      const set = new Set(this.historyList);
      //   set.delete(this.kw);
      //   set.add(this.kw);
      this.historyList = Array.from(set);
      console.log(this.historyList);
      setItem("kw", this.historyList);
    },
    cleanHistory() {
      // 清空 data 中保存的搜索历史
      this.historys = [];
      // 清空本地存储中记录的搜索历史
      setItem("kw", "[]");
    },
    gotoGoodsList(item) {},
  },
};
</script>
<style lang='less' scoped>
.van-search__content {
  border-radius: 20px;
}
.van-search__action {
  color: #e8605f;
}

.sugg-list {
  padding: 0 5px;

  .sugg-item {
    font-size: 12px;
    padding: 13px 0;
    border-bottom: 1px solid #efefef;
    display: flex;
    align-items: center;
    justify-content: space-between;

    .goods-name {
      // 文字不允许换行（单行文本）
      white-space: nowrap;
      // 溢出部分隐藏
      overflow: hidden;
      // 文本溢出后，使用 ... 代替
      text-overflow: ellipsis;
      margin-right: 3px;
    }
  }
}
.history-box {
  padding: 0 5px;

  .history-title {
    display: flex;
    justify-content: space-between;
    align-items: center;
    height: 40px;
    font-size: 13px;
    border-bottom: 1px solid #efefef;
  }

  .history-list {
    display: flex;
    flex-wrap: wrap;

    .van-tag {
      margin-top: 5px;
      margin-right: 5px;
    }
  }
}
</style>